clear
set more off

cap log close

**** Data ******

*** Children's names ***

use $tempdir/1850_fathers.dta, clear

keep if sex==1 & relate==3 & age>=0 & age<=15 & noname==0 
gen farmer = (occ1950 == 100)
egen name_count=count(sex), by(first)
collapse (mean) year name_count logweal_pers_father = logoccweal_pers_50_father logweal_padj_father = logoccweal_padj_50_father ///
logweal_padj2_father = logoccweal_padj2_50_father logweal_pxsth_father = logoccweal_pxsth_50_father ///
logweal_real_father = logoccweal_real_50_father logweal_radj_father = logoccweal_radj_50_father ///
logweal_rfull_father = logoccweal_rfull_50_father logweal_rfadj_father = logoccweal_rfadj_50_father ///
logweal_r50_father = logoccweal_r50_50_father logweal_r50adj_father = logoccweal_r50adj_50_father ///
logweal_r60_father = logoccweal_r60_50_father logweal_r60adj_father = logoccweal_r60adj_50_father ///
pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first)
sort first
gen sex = 1
gen year1 = 1850

*reg logweal_padj_father pct_farmer [fw = name_count]
*predict res_logweal_padj*_father, res


foreach var in $wealthvars{
xtile qtl_`var'_unwgt_full = logweal_`var'_father, nq(4)
xtile qtl_`var'_wgt_full = logweal_`var'_father [fw=name_count], nq(4)

xtile wealth_`var'_rank = logweal_`var'_father [fw=name_count], nq(100)
}


save $tempdir/childnames_wealth.dta, replace

use $tempdir/1850_fathers.dta, clear

gen region_birth_coarse = floor(region_birth/10)
replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
tab region_birth_coarse

keep if sex==1 & relate==3 & age>=0 & age<=15 & noname==0 
gen farmer = (occ1950 == 100)
egen name_count_byreg=count(sex), by(first)
collapse (mean) year name_count_byreg logweal_pers_father_reg = logoccweal_pers_50_father logweal_padj_father_reg = logoccweal_padj_50_father ///
logweal_padj2_father_reg = logoccweal_padj2_50_father logweal_pxsth_father_reg = logoccweal_pxsth_50_father ///
logweal_real_father_reg = logoccweal_real_50_father logweal_radj_father_reg = logoccweal_radj_50_father ///
logweal_rfull_father_reg = logoccweal_rfull_50_father logweal_rfadj_father_reg = logoccweal_rfadj_50_father ///
logweal_r50_father_reg = logoccweal_r50_50_father logweal_r50adj_father_reg = logoccweal_r50adj_50_father ///
logweal_r60_father_reg = logoccweal_r60_50_father logweal_r60adj_father_reg = logoccweal_r60adj_50_father ///
pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first region_birth_coarse)
sort first
gen sex = 1
gen year1 = 1850

*reg logweal_padj_father pct_farmer [fw = name_count]
*predict res_logweal_padj*_father, res


foreach var in $wealthvars{
xtile qtl_reg_`var'_unwgt_full = logweal_`var'_father, nq(4)
xtile qtl_reg_`var'_wgt_full = logweal_`var'_father [fw=name_count], nq(4)

xtile wealth_reg_`var'_rank = logweal_`var'_father [fw=name_count], nq(100)
}


save $tempdir/childnames_byregion_wealth.dta, replace

use $tempdir/1850_fathers.dta, clear
keep if sex==2 & relate==3 & age>=0 & age<=15 & noname==0 
gen farmer = (occ1950 == 100)
egen name_count=count(sex), by(first)
collapse (mean) year name_count logweal_pers_father = logoccweal_pers_50_father logweal_padj_father = logoccweal_padj_50_father ///
logweal_padj2_father = logoccweal_padj2_50_father logweal_pxsth_father = logoccweal_pxsth_50_father ///
logweal_real_father = logoccweal_real_50_father logweal_radj_father = logoccweal_radj_50_father ///
logweal_rfull_father = logoccweal_rfull_50_father logweal_rfadj_father = logoccweal_rfadj_50_father ///
logweal_r50_father = logoccweal_r50_50_father logweal_r50adj_father = logoccweal_r50adj_50_father ///
logweal_r60_father = logoccweal_r60_50_father logweal_r60adj_father = logoccweal_r60adj_50_father ///
pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first)
sort first
gen sex = 2
gen year1 = 1850

*reg logweal_padj_father pct_farmer [fw = name_count]
*predict res_logweal_padj*_father, res


foreach var in $wealthvars{
xtile qtl_`var'_unwgt_full = logweal_`var'_father, nq(4)
xtile qtl_`var'_wgt_full = logweal_`var'_father [fw=name_count], nq(4)

xtile wealth_`var'_rank = logweal_`var'_father [fw=name_count], nq(100)
}


append using $tempdir/childnames_wealth.dta
save $tempdir/childnames_wealth.dta, replace

use $tempdir/1850_fathers.dta, clear

gen region_birth_coarse = floor(region_birth/10)
replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
tab region_birth_coarse

keep if sex==2 & relate==3 & age>=0 & age<=15 & noname==0 
gen farmer = (occ1950 == 100)
egen name_count_byreg=count(sex), by(first)
collapse (mean) year name_count_byreg logweal_pers_father_reg = logoccweal_pers_50_father logweal_padj_father_reg = logoccweal_padj_50_father ///
logweal_padj2_father_reg = logoccweal_padj2_50_father logweal_pxsth_father_reg = logoccweal_pxsth_50_father ///
logweal_real_father_reg = logoccweal_real_50_father logweal_radj_father_reg = logoccweal_radj_50_father ///
logweal_rfull_father_reg = logoccweal_rfull_50_father logweal_rfadj_father_reg = logoccweal_rfadj_50_father ///
logweal_r50_father_reg = logoccweal_r50_50_father logweal_r50adj_father_reg = logoccweal_r50adj_50_father ///
logweal_r60_father_reg = logoccweal_r60_50_father logweal_r60adj_father_reg = logoccweal_r60adj_50_father ///
pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first region_birth_coarse)
sort first
gen sex = 2
gen year1 = 1850

*reg logweal_padj_father pct_farmer [fw = name_count]
*predict res_logweal_padj*_father, res


foreach var in $wealthvars{
xtile qtl_reg_`var'_unwgt_full = logweal_`var'_father_reg, nq(4)
xtile qtl_reg_`var'_wgt_full = logweal_`var'_father_reg [fw=name_count], nq(4)

xtile wealth_`var'_rank_reg = logweal_`var'_father_reg [fw=name_count], nq(100)
}


append using $tempdir/childnames_byregion_wealth.dta
save $tempdir/childnames_byregion_wealth.dta, replace

local s1850 "50"
local s1860 "60"
local s1870 "70"
local s1880 "80"
local s1900 "00"
local s1910 "10"
local s1920 "20"

foreach y in 1860 1870 1880 1900 1910 1920 {

	use $tempdir/`y'_fathers.dta, clear
	keep if sex==1 & relate==3 & age>=0 & age<=15 & noname==0 
	gen farmer = (occ1950 == 100)
	egen name_count=count(sex), by(first)
	collapse (mean) year name_count logweal_pers_father = logoccweal_pers_`s`y''_father logweal_padj_father = logoccweal_padj_`s`y''_father ///
	logweal_padj2_father = logoccweal_padj2_`s`y''_father logweal_pxsth_father = logoccweal_pxsth_`s`y''_father ///
	logweal_real_father = logoccweal_real_`s`y''_father logweal_radj_father = logoccweal_radj_`s`y''_father ///
	logweal_rfull_father = logoccweal_rfull_`s`y''_father logweal_rfadj_father = logoccweal_rfadj_`s`y''_father ///
	logweal_r50_father = logoccweal_r50_`s`y''_father logweal_r50adj_father = logoccweal_r50adj_`s`y''_father ///
	logweal_r60_father = logoccweal_r60_`s`y''_father logweal_r60adj_father = logoccweal_r60adj_`s`y''_father ///
	pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first)
	sort first
	gen sex = 1
	gen year1 = `y'
	
	*reg logweal_padj_father pct_farmer [fw = name_count]
	*predict res_logweal_padj*_father, res


	foreach var in $wealthvars{
		xtile qtl_`var'_unwgt_full = logweal_`var'_father, nq(4)
		xtile qtl_`var'_wgt_full = logweal_`var'_father [fw=name_count], nq(4)

		xtile wealth_`var'_rank = logweal_`var'_father [fw=name_count], nq(100)
		}

	append using $tempdir/childnames_wealth.dta
	save $tempdir/childnames_wealth.dta, replace

	use $tempdir/`y'_fathers.dta, clear
	keep if sex==2 & relate==3 & age>=0 & age<=15 & noname==0 
	gen farmer = (occ1950 == 100)
	egen name_count=count(sex), by(first)
	collapse (mean) year name_count logweal_pers_father = logoccweal_pers_`s`y''_father logweal_padj_father = logoccweal_padj_`s`y''_father ///
	logweal_padj2_father = logoccweal_padj2_`s`y''_father logweal_pxsth_father = logoccweal_pxsth_`s`y''_father ///
	logweal_real_father = logoccweal_real_`s`y''_father logweal_radj_father = logoccweal_radj_`s`y''_father ///
	logweal_rfull_father = logoccweal_rfull_`s`y''_father logweal_rfadj_father = logoccweal_rfadj_`s`y''_father ///
	logweal_r50_father = logoccweal_r50_`s`y''_father logweal_r50adj_father = logoccweal_r50adj_`s`y''_father ///
	logweal_r60_father = logoccweal_r60_`s`y''_father logweal_r60adj_father = logoccweal_r60adj_`s`y''_father ///
	pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first)
	sort first
	gen sex = 2
	gen year1 = `y'
	
	*reg logweal_padj_father pct_farmer [fw = name_count]
	*predict res_logweal_padj*_father, res


	foreach var in $wealthvars{
		xtile qtl_`var'_unwgt_full = logweal_`var'_father, nq(4)
		xtile qtl_`var'_wgt_full = logweal_`var'_father [fw=name_count], nq(4)

		xtile wealth_`var'_rank = logweal_`var'_father [fw=name_count], nq(100)
		}

	append using $tempdir/childnames_wealth.dta
	save $tempdir/childnames_wealth.dta, replace
	
	*** By region ****
	
	use $tempdir/`y'_fathers.dta, clear
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	tab region_birth_coarse

	keep if sex==1 & relate==3 & age>=0 & age<=15 & noname==0 
	gen farmer = (occ1950 == 100)
	egen name_count_byreg=count(sex), by(first)
	collapse (mean) year name_count_byreg logweal_pers_father_reg = logoccweal_pers_`s`y''_father logweal_padj_father_reg = logoccweal_padj_`s`y''_father ///
	logweal_padj2_father_reg = logoccweal_padj2_`s`y''_father logweal_pxsth_father_reg = logoccweal_pxsth_`s`y''_father ///
	logweal_real_father_reg = logoccweal_real_`s`y''_father logweal_radj_father_reg = logoccweal_radj_`s`y''_father ///
	logweal_rfull_father_reg = logoccweal_rfull_`s`y''_father logweal_rfadj_father_reg = logoccweal_rfadj_`s`y''_father ///
	logweal_r50_father_reg = logoccweal_r50_`s`y''_father logweal_r50adj_father_reg = logoccweal_r50adj_`s`y''_father ///
	logweal_r60_father_reg = logoccweal_r60_`s`y''_father logweal_r60adj_father_reg = logoccweal_r60adj_`s`y''_father ///
	pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first region_birth_coarse)
	sort first
	gen sex = 1
	gen year1 = `y'
	
	*reg logweal_padj_father pct_farmer [fw = name_count]
	*predict res_logweal_padj*_father, res


	foreach var in $wealthvars{
		xtile qtl_reg_`var'_unwgt_full = logweal_`var'_father_reg, nq(4)
		xtile qtl_reg_`var'_wgt_full = logweal_`var'_father_reg [fw=name_count], nq(4)

		xtile wealth_`var'_rank_reg = logweal_`var'_father_reg [fw=name_count], nq(100)
		}


	append using $tempdir/childnames_byregion_wealth.dta
	save $tempdir/childnames_byregion_wealth.dta, replace

	use $tempdir/`y'_fathers.dta, clear
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	tab region_birth_coarse
	
	keep if sex==2 & relate==3 & age>=0 & age<=15 & noname==0 
	gen farmer = (occ1950 == 100)
	egen name_count_byreg=count(sex), by(first)
	collapse (mean) year name_count_byreg logweal_pers_father_reg = logoccweal_pers_`s`y''_father logweal_padj_father_reg = logoccweal_padj_`s`y''_father ///
	logweal_padj2_father_reg = logoccweal_padj2_`s`y''_father logweal_pxsth_father_reg = logoccweal_pxsth_`s`y''_father ///
	logweal_real_father_reg = logoccweal_real_`s`y''_father logweal_radj_father_reg = logoccweal_radj_`s`y''_father ///
	logweal_rfull_father_reg = logoccweal_rfull_`s`y''_father logweal_rfadj_father_reg = logoccweal_rfadj_`s`y''_father ///
	logweal_r50_father_reg = logoccweal_r50_`s`y''_father logweal_r50adj_father_reg = logoccweal_r50adj_`s`y''_father ///
	logweal_r60_father_reg = logoccweal_r60_`s`y''_father logweal_r60adj_father_reg = logoccweal_r60adj_`s`y''_father ///
	pct_farmer = farmer logoccscore_father = logoccscore_*_father [aw=perwt], by(first region_birth_coarse)
	sort first
	gen sex = 2
	gen year1 = `y'
	
	*reg logweal_padj_father pct_farmer [fw = name_count]
	*predict res_logweal_padj*_father, res


	foreach var in $wealthvars{
		xtile qtl_reg_`var'_unwgt_full = logweal_`var'_father_reg, nq(4)
		xtile qtl_reg_`var'_wgt_full = logweal_`var'_father_reg [fw=name_count], nq(4)

		xtile wealth_`var'_rank_reg = logweal_`var'_father_reg [fw=name_count], nq(100)
		}


	append using $tempdir/childnames_byregion_wealth.dta
	save $tempdir/childnames_byregion_wealth.dta, replace	

}

***** 30-year*****

use $rawdir/1880_1%.dta, clear

	gen region_birth = 11 if bpl==9 | bpl==23 | bpl==25 | bpl==33 | bpl==44 | bpl==50
	replace region_birth = 12 if bpl==36 | bpl==34 | bpl==42
	replace region_birth = 21 if bpl==17 | bpl==18 | bpl==26 | bpl==39 | bpl==55
	replace region_birth = 22 if bpl==19 | bpl==20 | bpl==27 | bpl==29 | bpl==31 | bpl==38 | bpl==46
	replace region_birth = 31 if bpl== 10 | bpl==11 | bpl==24 | bpl==12 | bpl==13 | bpl==37 | bpl==45 | bpl==51 | bpl==54
	replace region_birth = 32 if bpl==1 | bpl==21 | bpl==28 | bpl==47
	replace region_birth = 33 if bpl==5 | bpl==22 | bpl==40 | bpl==48
	replace region_birth = 41 if bpl==4 | bpl==8 | bpl==16 | bpl==32 | bpl==30 | bpl==35 | bpl==56 | bpl==49
	replace region_birth = 42 if bpl==6 | bpl==41 | bpl==53
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	
	*** NAMES
	*** first name 
	gen str first=word(namefrst,1)
	replace first=subinstr(first, ".", "", .)
	replace first=trim(first)
	replace first=proper(first)
	*** middle name
	gen str middle=word(namefrst,2)
	replace middle=proper(middle)
	
	gen init = "X" if middle!=""
	
	*** obvious abbreviations (not nicknames)
	replace first="William" if first=="Wm"
	replace first="George" if first=="Geo"
	replace first="Charles" if first=="Chas"
	replace first="Daniel" if first=="Danl"
	replace first="James" if first=="Jas"
	replace first="Joseph" if first=="Jos"
	replace first="Robert" if first=="Robt"
	replace first="Richard" if first=="Richd"
	replace first="Samuel" if first=="Saml"
	replace first="Thomas" if first=="Thos"
	replace first="Frederick" if first=="Fredk"
	replace first="Frederick" if first=="Fred'K" 
	replace first="John" if first=="Jno" 
	replace first="Samuel" if first=="Sam'L"  
	replace first="Thomas" if first=="Tho" 
	replace first="Michael" if first=="Michl"

gen year1 = 1850
gen year2 = 1880

keep if age>=30 & age<=45

mer m:1 year1 sex first using $tempdir/childnames_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen linked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen linked_byreg = _merge==3
drop _merge

/*
egen tag = tag(first sex)
xtile x = logoccpweal_adj_father if tag==1 & sex==1, nq(4)
egen y = mean(x), by(first sex)

gen quartile_unwgt_linked = y if sex==1
drop x y

xtile x = logoccpweal_adj_father if tag==1 & sex==2, nq(4)
egen y = mean(x), by(first sex)

replace quartile_unwgt_linked = y if sex==2
drop x y tag

xtile x = logoccpweal_adj_father if sex==1, nq(4)
gen quartile_wgt_linked = x if sex==1
drop x

xtile x = logoccpweal_adj_father if sex==2, nq(4)
replace quartile_wgt_linked = x if sex==2
drop x 
*/

gen married = marst==1 | marst==2
gen ever_married = marst!=6

**** Assign husband's and wife's father's occwealth***

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbfirst = first if husb==1
replace husbfirst = husbfirst[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifefirst = first if wife==1
replace wifefirst = wifefirst[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbbpl = bpl if husb==1
replace husbbpl = husbbpl[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifebpl = bpl if wife==1
replace wifebpl = wifebpl[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbrace = race if husb==1
replace husbrace = husbrace[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wiferace = race if wife==1
replace wiferace = wiferace[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbage = age if husb==1
replace husbage = husbage[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifeage = age if wife==1
replace wifeage = wifeage[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spouseage = husbage if wife==1
replace spouseage = wifeage if husb==1

gen spousebpl = husbbpl if wife==1
replace spousebpl = wifebpl if husb==1

gen spouserace = husbrace if wife==1
replace spouserace = wiferace if husb==1

gen spousefirst = husbfirst if wife==1
replace spousefirst = wifefirst if husb==1
gen spousesex = 1 if wife==1
replace spousesex = 2 if husb==1

gen spouseregion_birth = 11 if spousebpl==9 | spousebpl==23 | spousebpl==25 | spousebpl==33 | spousebpl==44 | spousebpl==50
replace spouseregion_birth = 12 if spousebpl==36 | spousebpl==34 | spousebpl==42
replace spouseregion_birth = 21 if spousebpl==17 | spousebpl==18 | spousebpl==26 | spousebpl==39 | spousebpl==55
replace spouseregion_birth = 22 if spousebpl==19 | spousebpl==20 | spousebpl==27 | spousebpl==29 | spousebpl==31 | spousebpl==38 | spousebpl==46
replace spouseregion_birth = 31 if spousebpl== 10 | spousebpl==11 | spousebpl==24 | spousebpl==12 | spousebpl==13 | spousebpl==37 | spousebpl==45 | spousebpl==51 | spousebpl==54
replace spouseregion_birth = 32 if spousebpl==1 | spousebpl==21 | spousebpl==28 | spousebpl==47
replace spouseregion_birth = 33 if spousebpl==5 | spousebpl==22 | spousebpl==40 | spousebpl==48
replace spouseregion_birth = 41 if spousebpl==4 | spousebpl==8 | spousebpl==16 | spousebpl==32 | spousebpl==30 | spousebpl==35 | spousebpl==56 | spousebpl==49
replace spouseregion_birth = 42 if spousebpl==6 | spousebpl==41 | spousebpl==53
	
gen spouseregion_birth_coarse = floor(spouseregion_birth/10)
replace spouseregion_birth_coarse = 5 if spousebpl>=100 & spousebpl!=.

rename sex ownsex
rename first ownfirst
rename spousefirst first
rename spousesex sex

foreach var in name_count* logweal* qtl* wealth* logoccscore* {
	rename `var' own`var'
}

mer m:1 year1 sex first using $tempdir/childnames_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen spouselinked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen spouselinked_byreg = _merge==3
drop _merge

rename first spousefirst
rename sex spousesex
rename ownsex sex
rename ownfirst first	

foreach var in name_count* logweal* qtl* wealth* logoccscore*{
	rename `var' `var'_spouse
	rename own`var' `var'
}

foreach var in $wealthvars{
	gen rankweal_`var'_father = .
	xtile x = logweal_`var'_father if sex==1, nq(100)
	xtile y = logweal_`var'_father if sex==2, nq(100)
	replace rankweal_`var'_father = x if sex==1
	replace rankweal_`var'_father = y if sex==2
	
	gen rankweal_`var'_father_spouse = .
	xtile w = logweal_`var'_father_spouse if sex==1, nq(100)
	xtile z = logweal_`var'_father_spouse if sex==2, nq(100)
	replace rankweal_`var'_father_spouse = w if sex==1
	replace rankweal_`var'_father_spouse = z if sex==2

	drop w x y z
}

save $tempdir/panel_trends_30yr_wealth.dta, replace

foreach y in $varlistshort {

	use $rawdir/`y'_1%.dta, clear
	
	/*
	if `y'==1940 {
	
		drop if serial==""
		
		rename serial serial_string
		egen serial = group(serial_string)
		drop serial_string
		rename statefips statefip
		rename hhorder pernum
		
		gen region = 11 if statefip==9 | statefip==23 | statefip==25 | statefip==33 | statefip==44 | statefip==50
		replace region = 12 if statefip==36 | statefip==34 | statefip==42
		replace region = 21 if statefip==17 | statefip==18 | statefip==26 | statefip==39 | statefip==55
		replace region = 22 if statefip==19 | statefip==20 | statefip==27 | statefip==29 | statefip==31 | statefip==38 | statefip==46
		replace region = 31 if statefip== 10 | statefip==11 | statefip==24 | statefip==12 | statefip==13 | statefip==37 | statefip==45 | statefip==51 | statefip==54
		replace region = 32 if statefip==1 | statefip==21 | statefip==28 | statefip==47
		replace region = 33 if statefip==5 | statefip==22 | statefip==40 | statefip==48
		replace region = 41 if statefip==4 | statefip==8 | statefip==16 | statefip==32 | statefip==30 | statefip==35 | statefip==56 | statefip==49
		replace region = 42 if statefip==6 | statefip==41 | statefip==53
		
		rename bpl bpl_orig
		
		mer m:1 bpl_orig using $rawdir/1940_bpls.dta, keepus(bpl_string bpl)
		drop _merge
		
		replace bpl = statefip if bpl==0
		
		do $rawdir/labels_ipums.do
		
		label values statefip bpl_lbl
		
		sort serial pernum
		gen sploc = 2 if sex==1 & relate==1 & marst==1 & sex[_n+1]==2 & relate[_n+1]==2 & marst[_n+1]==1
		replace sploc = 1 if sex==2 & relate==2 & marst==1 & sex[_n-1]==1 & relate[_n-1]==1 & marst[_n-1]==1
		replace sploc = 0 if sploc==.
		
		keep namefrst sex age marst sploc relate statefip region bpl serial pernum race
		
	}	
	*/

	gen region_birth = 11 if bpl==9 | bpl==23 | bpl==25 | bpl==33 | bpl==44 | bpl==50
	replace region_birth = 12 if bpl==36 | bpl==34 | bpl==42
	replace region_birth = 21 if bpl==17 | bpl==18 | bpl==26 | bpl==39 | bpl==55
	replace region_birth = 22 if bpl==19 | bpl==20 | bpl==27 | bpl==29 | bpl==31 | bpl==38 | bpl==46
	replace region_birth = 31 if bpl== 10 | bpl==11 | bpl==24 | bpl==12 | bpl==13 | bpl==37 | bpl==45 | bpl==51 | bpl==54
	replace region_birth = 32 if bpl==1 | bpl==21 | bpl==28 | bpl==47
	replace region_birth = 33 if bpl==5 | bpl==22 | bpl==40 | bpl==48
	replace region_birth = 41 if bpl==4 | bpl==8 | bpl==16 | bpl==32 | bpl==30 | bpl==35 | bpl==56 | bpl==49
	replace region_birth = 42 if bpl==6 | bpl==41 | bpl==53
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	
	*** NAMES
	*** first name 
	gen str first=word(namefrst,1)
	replace first=subinstr(first, ".", "", .)
	replace first=trim(first)
	replace first=proper(first)
	*** middle name
	gen str middle=word(namefrst,2)
	replace middle=proper(middle)
	
	gen init = "X" if middle!=""
	
	*** obvious abbreviations (not nicknames)
	replace first="William" if first=="Wm"
	replace first="George" if first=="Geo"
	replace first="Charles" if first=="Chas"
	replace first="Daniel" if first=="Danl"
	replace first="James" if first=="Jas"
	replace first="Joseph" if first=="Jos"
	replace first="Robert" if first=="Robt"
	replace first="Richard" if first=="Richd"
	replace first="Samuel" if first=="Saml"
	replace first="Thomas" if first=="Thos"
	replace first="Frederick" if first=="Fredk"
	replace first="Frederick" if first=="Fred'K" 
	replace first="John" if first=="Jno" 
	replace first="Samuel" if first=="Sam'L"  
	replace first="Thomas" if first=="Tho" 
	replace first="Michael" if first=="Michl"

gen year1 = `y'-30
gen year2 = `y'

keep if age>=30 & age<=45

mer m:1 year1 sex first using $tempdir/childnames_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen linked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen linked_byreg = _merge==3
drop _merge

/*
egen tag = tag(first sex)
xtile x = logoccpweal_adj_father if tag==1 & sex==1, nq(4)
egen y = mean(x), by(first sex)

gen quartile_unwgt_linked = y if sex==1
drop x y

xtile x = logoccpweal_adj_father if tag==1 & sex==2, nq(4)
egen y = mean(x), by(first sex)

replace quartile_unwgt_linked = y if sex==2
drop x y tag

xtile x = logoccpweal_adj_father if sex==1, nq(4)
gen quartile_wgt_linked = x if sex==1
drop x

xtile x = logoccpweal_adj_father if sex==2, nq(4)
replace quartile_wgt_linked = x if sex==2
drop x 
*/

gen married = marst==1 | marst==2
gen ever_married = marst!=6

**** Assign husband's and wife's father's occwealth ***

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbfirst = first if husb==1
replace husbfirst = husbfirst[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifefirst = first if wife==1
replace wifefirst = wifefirst[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbbpl = bpl if husb==1
replace husbbpl = husbbpl[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifebpl = bpl if wife==1
replace wifebpl = wifebpl[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbrace = race if husb==1
replace husbrace = husbrace[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wiferace = race if wife==1
replace wiferace = wiferace[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbage = age if husb==1
replace husbage = husbage[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifeage = age if wife==1
replace wifeage = wifeage[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spouseage = husbage if wife==1
replace spouseage = wifeage if husb==1

gen spousebpl = husbbpl if wife==1
replace spousebpl = wifebpl if husb==1

gen spouserace = husbrace if wife==1
replace spouserace = wiferace if husb==1

gen spousefirst = husbfirst if wife==1
replace spousefirst = wifefirst if husb==1
gen spousesex = 1 if wife==1
replace spousesex = 2 if husb==1

gen spouseregion_birth = 11 if spousebpl==9 | spousebpl==23 | spousebpl==25 | spousebpl==33 | spousebpl==44 | spousebpl==50
replace spouseregion_birth = 12 if spousebpl==36 | spousebpl==34 | spousebpl==42
replace spouseregion_birth = 21 if spousebpl==17 | spousebpl==18 | spousebpl==26 | spousebpl==39 | spousebpl==55
replace spouseregion_birth = 22 if spousebpl==19 | spousebpl==20 | spousebpl==27 | spousebpl==29 | spousebpl==31 | spousebpl==38 | spousebpl==46
replace spouseregion_birth = 31 if spousebpl== 10 | spousebpl==11 | spousebpl==24 | spousebpl==12 | spousebpl==13 | spousebpl==37 | spousebpl==45 | spousebpl==51 | spousebpl==54
replace spouseregion_birth = 32 if spousebpl==1 | spousebpl==21 | spousebpl==28 | spousebpl==47
replace spouseregion_birth = 33 if spousebpl==5 | spousebpl==22 | spousebpl==40 | spousebpl==48
replace spouseregion_birth = 41 if spousebpl==4 | spousebpl==8 | spousebpl==16 | spousebpl==32 | spousebpl==30 | spousebpl==35 | spousebpl==56 | spousebpl==49
replace spouseregion_birth = 42 if spousebpl==6 | spousebpl==41 | spousebpl==53
	
gen spouseregion_birth_coarse = floor(spouseregion_birth/10)
replace spouseregion_birth_coarse = 5 if spousebpl>=100 & spousebpl!=.

rename sex ownsex
rename first ownfirst
rename spousefirst first
rename spousesex sex

foreach var in name_count* logweal* qtl* wealth* logoccscore* {
	rename `var' own`var'
}

mer m:1 year1 sex first using $tempdir/childnames_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen spouselinked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_wealth.dta, keepus(name_count* logweal* qtl* wealth* logoccscore*)
drop if _merge==2
gen spouselinked_byreg = _merge==3
drop _merge

rename first spousefirst
rename sex spousesex
rename ownsex sex
rename ownfirst first	

foreach var in name_count* logweal* qtl* wealth* logoccscore*{
	rename `var' `var'_spouse
	rename own`var' `var'
}

foreach var in $wealthvars{
	gen rankweal_`var'_father = .
	xtile x = logweal_`var'_father if sex==1, nq(100)
	xtile y = logweal_`var'_father if sex==2, nq(100)
	replace rankweal_`var'_father = x if sex==1
	replace rankweal_`var'_father = y if sex==2
	
	gen rankweal_`var'_father_spouse = .
	xtile w = logweal_`var'_father_spouse if sex==1, nq(100)
	xtile z = logweal_`var'_father_spouse if sex==2, nq(100)
	replace rankweal_`var'_father_spouse = w if sex==1
	replace rankweal_`var'_father_spouse = z if sex==2

	drop w x y z
}
	
	append using $tempdir/panel_trends_30yr_wealth.dta
	save $tempdir/panel_trends_30yr_wealth.dta, replace
	
}

